package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //新增菜品，同时插入菜品对应的口味数据，需要操作两张表：dish、dish_flavor
    @Override
    @Transactional
    public void saveDishWithFlavors(DishDto dishDto) {
        //保存菜品数据
        dishMapper.insert(dishDto);
        //得到传过来的口味集合
        List<DishFlavor> flavors = dishDto.getFlavors();

        //判断是否选择了口味
        if(flavors!=null && flavors.size()>0){
            //保存菜品的口味数据 需要注意：DishFlavor中dishId是没有数据的，
            // 所以需要手动设置

            Long dishId = dishDto.getId();
            log.info("新增的菜品id是" + dishId);
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    //根据id查询菜品信息和对应的口味信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息,从dish表查询
        Dish dish = dishMapper.selectById(id);

        //将查到的dish中的属性拷贝到dishDto中
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询当前菜品对应的口味信息,从dish_flavor表查询
        //创建一个条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorMapper.selectList(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    //更新菜品信息,同时更新对应的口味信息
    @Override
    public void updateDishWithFlavor(DishDto dishDto) {
        //更新dish表中的信息
        dishMapper.updateById(dishDto);

        //创建一个条件构造器,先清除里面当前菜品对应的风味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        //清除风味
        dishFlavorMapper.delete(queryWrapper);

        //得到里面的风味
        List<DishFlavor> flavors = dishDto.getFlavors();

        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorMapper.insert(flavor);
        }
    }

    @Override
    public List<DishDto> listWithFlavor(Dish dish) {

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Dish::getStatus,1);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishes = dishMapper.selectList(queryWrapper);

        List<DishDto> dishDtos = dishes.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtil.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            //根据id查询分类对象
            Category category = categoryMapper.selectById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                //将得到的分类名称设置入dishDto对象中
                dishDto.setCategoryName(categoryName);
            }

            //得到当前菜品的id
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
            qw.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> flavors = dishFlavorMapper.selectList(qw);

            //将得到的口味集合名称设置入dishDto对象中
            dishDto.setFlavors(flavors);

            return dishDto;
        }).collect(Collectors.toList());


        return dishDtos;
    }


}
